<?php

namespace V1\Model;


use Think\Exception;
use Think\Model;

class ExpertProfileModel extends Model
{
    protected $tableName = 'expert_profile';

    /**
     * 获取专家信息
     * @param $userid
     * @param array $field
     * @return mixed
     */
    public function get_expert_profile($userid, $field = array())
    {
        $map['userid'] = !empty($userid) ? $userid : "";
        $member = $this->where($map)->field($field)->find();
        if(false===$member){
            throw new Exception(C('STATUS')['101']);
        }else{
            return $member;
        }
    }

    /**
     * 输出用户勋章
     * @param $id
     * @return array
     */
    public function return_rank($id)
    {
        $map['userid'] = !empty($id) ? $id : '';
        //判断是否进行职称认证
        $certification = $this->get_expert_type($id);
        //判断是否为特聘专家
        $special = $this->get_special($id);
        //判断是否为优秀专家
        $excellent = $this->get_excellent($id);
        //判断是否为回答达人
        $answer = $this->get_answerList($id);
        //判断是否为威望达人
        $prestige = $this->get_member_attention($id);
        $data = ['certification_type' => $certification, 'special_type' => $special, 'answer_type' => $answer, 'excellent_type' => $excellent, 'prestige_type' => $prestige];
        return $data;
    }

    /**
     * 获取是否为认证专家
     * @param $uid
     * @return int
     */
    private function get_expert_type($uid)
    {
        $model = D('ExpertProfile');
        $map['userid'] = $uid;
        $data = $model->where($map)->count();
        return $data == 1 ? 1 : 0;
    }

    /**
     * 获取是否为特聘专家
     * @param $uid
     * @return int
     */
    private function get_special($uid)
    {
        $model = D('ExpertProfile');
        $map['userid'] = $uid;
        $data = $model->where($map)->field('special')->find();
        return $data['special'] == 1 ? 1 : 0;
    }

    /**
     * 获取是否为回答达人
     * @param $uid
     * @return int
     */
    private function get_answerList($uid)
    {
        $count = D('QuestionAnswer')
            ->field('askid')
            ->where(array('uid' => $uid))
            ->order('addtime desc')
            ->count('askid');
        return $count > 60 ? 1 : 0;
    }

    /**
     * 获取是否为优秀专家
     */
    private function get_excellent($uid)
    {
        $question_answer_model = new QuestionAnswerModel();
        $data = $question_answer_model->add_adoption_rates($uid);
        $data = substr($data, 0, strlen($data) - 1);
        return $data >= 60 ? 1 : 0;
    }

    /**
     * 获取是否为威望达人
     * @param $uid
     * @return int
     */
    private function get_member_attention($uid)
    {
        $count = D('MemberFans')->where(array('fans_uid' => $uid))->count();
        return $count > 500 ? 1 : 0;
    }

    /**
     * Invite Expert
     * @param $info
     * @return mixed
     */
    public function getInviteExpert($info)
    {
        $member_fans_model = new MemberFansModel();
        $data = $this->alias('expert')
            ->join('LEFT JOIN __MEMBER_PROFILE__ member ON member.userid = expert.userid')
            ->join('LEFT JOIN __MESSAGE_INVITE__ message ON message.to_uid = expert.userid')
            ->where('expert.`status`=1')
            ->field('expert.userid,expert.`name`,expert.good_at_crop,member.avatar,member.truename,message.to_uid')
            ->group('expert.userid')
            ->order('member.score desc')
            ->select();
        foreach ($data as $k => $v) {
            $map['uid'] = $v['userid'];
            $map['agree_times'] = ['gt', '0'];
            $where['attention_uid'] = $v['userid'];
            $data2 = M('question_answer')->field('SUM(agree_times) AS agreed_times')->where($map)->select();
            $fans_count = $member_fans_model->get_fans_number($v['userid']);
            $data[$k] = $v;
            $data[$k]['agreed_times'] = $data2[0]['agreed_times'];
            $data[$k]['fans_nums'] = $fans_count;

            $arr = array();
            $arr['invitation_uid'] = $info['invitation_uid'];       //当前用户ID
            $arr['invite_uid'] = $v['userid'];                       //专家ID
            $arr['ask_id'] = $info['ask_id'];                         //问答ID

            //判断是否邀请过
            $message_model = new MessageModel();
            if ($message_model->check_invite_expert($arr) == 217) {
                $data[$k]['status'] = 1;
            } else {
                $data[$k]['status'] = 0;
            }
        }
        return $data;
    }
}